<?php
if (realpath($system_path) !== FALSE)
{
	$system_path = realpath($system_path).'/';
}

// ensure there's a trailing slash
$system_path = rtrim($system_path, '/').'/';

define('BASEPATH', str_replace("\\", "/", $system_path));
define('EXT','.php');

//ob_start();
//ob_implicit_flush(0);
session_start();
/*
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */

error_reporting(E_ALL|E_STRICT);
//error_reporting(0);
//error_reporting(E_ALL|E_STRICT);
/*

if(!isset($_SESSION['user']))
{
	if(isset($_COOKIE['__bhp_user']))
	{
		$cookie	=	json_decode($_COOKIE['__bhp_user']);
		if($cookie)
		{
			$_SESSION['user']	=	array('username' => $cookie->username,'email' => $cookie->email,'fullname' => $cookie->fullname);
		}
	}
}
*/

include_once 'detect_cookie.php';

//defined('BASE_PATH') || define('BASE_PATH', realpath(dirname(__FILE__)));
include_once ("config/config.php");
include_once ("config/database.php");
include_once ("config/smarty.php");
include_once ("config/common.php");
include_once ('lib/url_helper.php');
include_once 'lib/module.php';
include_once 'lib/load.php';


define('MOD_DIR', BASEPATH.'mod/');
define('MODEL_DIR', MOD_DIR.'models/');
define('HELPER_DIR', MOD_DIR.'helpers/');

$load	=	new load();

$module	=	new	module();

//ob_start("replaceTitle");
if (isset($_GET ['path'])) {
    $page = filter ($_GET ['path']);
}else {
    $page = 'home';
}
$params = explode('/', $page);
//$params[0] is difine for page
//$params[1] is difine for module
if($params[0]== $config ['admin_section'] || $params[0]=='favicon.ico')
{//admin
	$p	=	$params[1];
	define('TMPL_DIR', admin_path().'tmpl/');
	$file	=	admin_path().$p.EXT;
}
else 
{//client
	$p	=	$params[0];
	define('TMPL_DIR', client_path().'tmpl/');
	$file	=	client_path().$p.EXT;
}

$func	=	'index';

if(isset($params[0]))
{
	$module->set_class($params[0]);
}

if(isset($params[1]))
{
	$func	=	$params[1];
	$module->set_function($params[1]);
}

if(file_exists($file))
{
	include_once $file;
	//Call Controller
	$class	=	$p.'Controller';
	if(class_exists($class)){

		$obj	=	new	$class();
		if(method_exists($class, $func))
			$obj->$func();
	}
	//Call Controller
}
else
{
	exit('Page not existed.');
}


//ob_end_flush();
//print_gzipped_page();
?>